package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.QueryDonationRes;
import sz.szsmk.citizencard.ui.activity.DonationActivity;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2019
 * author: zdh
 * created on: 2019/1/5 13:49
 * description:
 */
public class DonationAdapter extends BaseRecyclerAdapter<DonationAdapter.DonationHolder> {

    private Context mContext;
    private List<QueryDonationRes.CommonwealOrdersBean> viewData;

    public DonationAdapter(DonationActivity donationActivity) {
        mContext = donationActivity;
    }

//    @Override
//    public int getItemViewType(int position) {
//        return R.layout.donation_view;
//    }

    @Override
    public DonationHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.donation_view, parent, false);
        return new DonationHolder(view);
    }

    @Override
    public void onBindViewHolder(final DonationHolder holder, final int position) {

        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        DonationItemAdapter donationItemAdapter = new DonationItemAdapter(mContext);
        holder.donationRv.setLayoutManager(manager);
        donationItemAdapter = new DonationItemAdapter(mContext);
        holder.donationRv.setAdapter(donationItemAdapter);
        if (viewData != null) {
            QueryDonationRes.CommonwealOrdersBean ordersBean = viewData.get(position);
            double totalMoney = (double) ordersBean.getTotalMoney() / 100;
//            double totalMoney = (double) ordersBean.getTotalMoney() / 100;
            holder.money.setText(totalMoney + "");
            holder.times.setText(ordersBean.getToatalTimes() + "");
            donationItemAdapter.setViewData(ordersBean.getRecordInfors());
            donationItemAdapter.notifyDataSetChanged();

            Glide.with(mContext).load(viewData.get(position).getPicUrl()).into(holder.donationIv);
        }

        holder.btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转查看公益项目
                Bundle bundle = new Bundle();
                String url = Constant.BASE_URL + "app-front-end/#/card/donationDetail?id=" + viewData.get(position).getCommonwealId()+"&isMain=1";
                bundle.putString("url", url);
                JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
            }
        });

        holder.btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (holder.donationRv.getVisibility() == View.VISIBLE) {
                    holder.donationRv.setVisibility(View.GONE);
                } else {
                    holder.donationRv.setVisibility(View.VISIBLE);
                }
                Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.arrow_set);
                animation.setInterpolator(new LinearInterpolator());
//                animation.setFillAfter(animation.getFillBefore());
                animation.setFillAfter(false);
                holder.arrowBlue.startAnimation(animation);
            }
        });
    }

    @Override
    public int getItemCount() {
        return null == viewData ? 0 : viewData.size();
    }

    public void setViewData(List<QueryDonationRes.CommonwealOrdersBean> viewData) {
        this.viewData = viewData;
    }


    public static class DonationHolder extends BaseHolder {

        private final LinearLayout btn2;
        private final ImageView arrowBlue;
        private final ImageView donationIv;
        private final RecyclerView donationRv;
        private final TextView money;
        private final TextView times;
        private final TextView btn1;

        public DonationHolder(View itemView) {
            super(itemView);
            arrowBlue = (ImageView) itemView.findViewById(R.id.arrow_blue);
            donationIv = (ImageView) itemView.findViewById(R.id.donation_item_iv);
            btn2 = (LinearLayout) itemView.findViewById(R.id.donation_item_btn2);
            donationRv = (RecyclerView) itemView.findViewById(R.id.donation_item_rv);
            money = (TextView) itemView.findViewById(R.id.donation_item_money);
            times = (TextView) itemView.findViewById(R.id.donation_item_times);
            btn1 = (TextView) itemView.findViewById(R.id.donation_item_btn1);

        }
    }

}
